#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
为citing_articles.txt文件中的每篇文章的引用添加编号
将格式从 "文章标题,URL" 转换为 "文章标题,编号,URL"
"""

import os
from collections import defaultdict

def add_numbering_to_citations():
    """为引用文章添加编号"""
    
    input_file = "data/citing_articles.txt"
    output_file = "data/citing_articles_numbered.txt"
    
    if not os.path.exists(input_file):
        print(f"错误：找不到输入文件 {input_file}")
        return
    
    # 用于存储每篇文章的引用
    articles_citations = defaultdict(list)
    
    print("正在读取文件...")
    
    # 读取文件并按文章分组
    with open(input_file, 'r', encoding='utf-8') as f:
        for line_num, line in enumerate(f, 1):
            line = line.strip()
            if not line:
                continue
                
            # 分割文章标题和URL
            parts = line.split(',', 1)  # 只分割第一个逗号
            if len(parts) != 2:
                print(f"警告：第{line_num}行格式不正确，跳过: {line}")
                continue
                
            article_title, url = parts
            articles_citations[article_title].append(url)
    
    print(f"找到 {len(articles_citations)} 篇不同的文章")
    
    # 统计信息
    total_citations = sum(len(citations) for citations in articles_citations.values())
    print(f"总共 {total_citations} 条引用")
    
    # 生成带编号的输出
    print("正在生成带编号的文件...")
    
    with open(output_file, 'w', encoding='utf-8') as f:
        processed_articles = 0
        
        for article_title, citations in articles_citations.items():
            processed_articles += 1
            
            # 为每篇文章的引用添加编号
            for i, url in enumerate(citations, 1):
                # 使用两位数格式（01, 02, 03...）
                number = str(i).zfill(2)
                formatted_line = f"{article_title},{number},{url}\n"
                f.write(formatted_line)
            
            # 显示进度
            if processed_articles % 10 == 0:
                print(f"已处理 {processed_articles}/{len(articles_citations)} 篇文章")
    
    print(f"\n✅ 处理完成！")
    print(f"📁 输入文件: {input_file}")
    print(f"📁 输出文件: {output_file}")
    print(f"📊 处理统计:")
    print(f"   - 文章数量: {len(articles_citations)}")
    print(f"   - 引用总数: {total_citations}")
    
    # 显示一些示例
    print(f"\n📋 格式示例:")
    with open(output_file, 'r', encoding='utf-8') as f:
        for i, line in enumerate(f):
            if i < 3:  # 显示前3行
                print(f"   {line.strip()}")
            else:
                break
    
    # 显示每篇文章的引用数量统计
    print(f"\n📈 引用数量分布（前10篇文章）:")
    sorted_articles = sorted(articles_citations.items(), key=lambda x: len(x[1]), reverse=True)
    for i, (title, citations) in enumerate(sorted_articles[:10]):
        short_title = title[:50] + "..." if len(title) > 50 else title
        print(f"   {i+1:2d}. {short_title} ({len(citations)} 条引用)")

if __name__ == "__main__":
    add_numbering_to_citations()